package com.xx566.socket;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.*;
import java.net.Socket;

public class LoginActivity extends Activity {

    private Button loginBtn;
    private EditText loginName;
    PrintWriter outputStream;
    BufferedReader inputStream;
    Socket socket;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //通过handler处理接收到的消息
            if (msg.what == 1) {
                //跳转到主页面，显示登录结果
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("result", msg.getData());
                startActivity(intent);
            }
        }
    };

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //登录按钮点击事件
        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginName = (EditText) findViewById(R.id.loginName);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击登录后，显示正在连接服务器
                loginBtn.setText("正在连接,请稍候...");
                loginBtn.setClickable(false);
                final String userName = loginName.getText().toString();
                //通过Socket登录服务器，简单的传递用户名
                new Thread() {
                    @Override
                    public void run() {
                        //处理接收到的消息
                        Message message = new Message();
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        String result = "";
                        try {
                            socket = new Socket("192.168.0.32", 8888);
                            outputStream = new PrintWriter(socket.getOutputStream());
                            inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                            outputStream.println(userName);
                            outputStream.flush();
                            //读取结果
                            while (true) {
                                result = inputStream.readLine();
                                if (!"".equals(result)) {
                                    break;
                                }
                            }
                            //关闭
                            inputStream.close();
                            outputStream.close();
                            socket.close();
                        } catch (Exception e) {
                            result = "网络异常！";
                        }
                        bundle.putString("result", result);
                        message.setData(bundle);
                        //传递消息
                        handler.sendMessage(message);
                    }
                }.start();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        loginBtn.setText("登录");
        loginBtn.setClickable(true);
    }
}
